////////////////////////////////////////////////
// DESCRIPTION:
//    Support classes for generating documentation
//
// Legal Notices:
//   Copyright (c) 2008, Telliam Consulting, LLC.
//   All rights reserved.
//
//   Redistribution and use in source and binary forms, with or without modification,
//   are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice, this list
//     of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice, this
//     list of conditions and the following disclaimer in the documentation and/or other
//     materials provided with the distribution.
//   * Neither the name of Telliam Consulting nor the names of its contributors may be
//     used to endorse or promote products derived from this software without specific
//     prior written permission.
//
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
//   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
//   SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
//   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
//   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
//   DAMAGE. 
//
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.XPath;
using System.Reflection;
using System.Diagnostics;
using System.Configuration;
using Microsoft.Ddue.Tools;     // Build component namespace
using System.Windows.Forms;     // For SHFB configuration
using System.Xml.Serialization;
using System.Collections.Generic;

namespace FusionWare.Sandcastle.Components
{
    public class MSHelpAttr : BuildComponent
    {
        #region Private data members
        List<KeyValuePair<string,string>> Attributes = new List<KeyValuePair<string,string>>();
        #endregion

        #region Constructor
        /// <summary>Constructor</summary>
        /// <param name="assembler">A reference to the build assembler.</param>
        /// <param name="configuration">The configuration information</param>
        /// <exception cref="ConfigurationErrorsException">indicates an error in the configuration.</exception>
        public MSHelpAttr(BuildAssembler assembler, XPathNavigator configuration)
            : base(assembler, configuration)
        {
            // Report version information at construction
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);

            base.WriteMessage(MessageLevel.Info, String.Format("[{0}, version {1}]\nMSHelpAttr Component. " +
                "Copyright (c) 2008, Telliam Consulting LLC., All rights reserved.",
                fvi.ProductName, fvi.ProductVersion));

            XPathNodeIterator attrNodes = configuration.Select("//MSHelpAttr");
            foreach(XPathNavigator docset in attrNodes)
            {
                string name = docset.GetAttribute("Name", string.Empty);
                string value = docset.GetAttribute("Value", string.Empty);
                base.WriteMessage(MessageLevel.Info, string.Format("Attribute {0}={1}",name, value)); 
                this.Attributes.Add(new KeyValuePair<string, string>(name,value));
            }
        }
        #endregion

        #region Apply the component
        /// <summary>This is implemented to perform the component tasks.</summary>
        /// <param name="document">The XML document with which to work.</param>
        /// <param name="key">The key (member name) of the item being documented.</param>
        public override void Apply(XmlDocument document, string key)
        {
            XPathNavigator node = document.CreateNavigator().SelectSingleNode(@"html/head/xml");
            foreach(KeyValuePair<string, string> pair in this.Attributes)
                node.AppendChild(string.Format("<MSHelp:Attr Name=\"{0}\" Value=\"{1}\" />", pair.Key, pair.Value));
        }
        #endregion

        #region Static configuration method for use with SHFB
        /// <summary>
        /// This static method is used by the Sandcastle Help
        /// File Builder to let the component perform its own
        /// configuration.
        /// </summary>
        /// <param name="currentConfig">The current configuration XML fragment</param>
        /// <returns>A string containing the new configuration XML fragment</returns>
        public static string ConfigureComponent(string currentConfig)
        {
            // Open the dialog to edit the configuration
            using(MSHelpAttrConfigDlg dlg = new MSHelpAttrConfigDlg(currentConfig))
            {
                // Get the modified configuration if OK was clicked
                if(dlg.ShowDialog() == DialogResult.OK)
                {
                        StringBuilder buffer = new StringBuilder();
                        XmlWriterSettings settings = new XmlWriterSettings();
                        settings.Indent = true;
                        settings.OmitXmlDeclaration = true;
                        XmlWriter writer = XmlWriter.Create(buffer,settings);
                        dlg.Config.Save(writer);
                        currentConfig = buffer.ToString();
                }
            }

            // Return the configuration data
            return currentConfig;
        }
        #endregion
    }
}
